﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

public partial class BusProjectReg : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            if (Session["user"] != null)
            {
                this.Log_in1.Visible = false;
                this.Log_out1.Visible = true;
            }
            else
            {
                Response.Redirect("Login.aspx");
            }

            this.btnSave.Visible = false;
            this.btnAdd.Enabled = true;
            //load BusCataloge List
            this.BindGrid();
        }
        else
        {
        }
    }
    public void BindGrid()
    {
        //load BusCataloge List
        ProBusCat pbc = new ProBusCat();
        DataSet ds = pbc.LoadBusCat();

        if (ds.Tables[0].Rows.Count == 0)
        {
            ds.Tables[0].Rows.Add(ds.Tables[0].NewRow());
            this.grdListBusCat.DataSource = ds.Tables[0];
            this.grdListBusCat.DataBind();
            this.grdListBusCat.Rows[0].Visible = false;
        }
        else
        {
            //ds.Tables[0].Columns["date"].DataType = Type.;
            this.grdListBusCat.DataSource = ds.Tables[0];
            this.grdListBusCat.DataBind();
            this.grdListBusCat.RowStyle.HorizontalAlign = HorizontalAlign.Center;
            this.grdListBusCat.HorizontalAlign = HorizontalAlign.Center;
            this.grdListBusCat.HeaderRow.Cells[0].Width = 20;
            this.grdListBusCat.HeaderRow.Cells[1].Width = 20;
            this.grdListBusCat.HeaderRow.Cells[2].Width = 30;
            this.grdListBusCat.HeaderRow.Cells[2].Text = "ID";
            this.grdListBusCat.HeaderRow.Cells[3].Text = "Name of Business Project";
            this.grdListBusCat.HeaderRow.Cells[3].Width = 200;
        }
    }
    protected void grdListBusCat_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        this.grdListBusCat.SelectedIndex = Int32.Parse(e.CommandArgument.ToString());
        this.lblError.Text = "";
    }
    protected void grdListBusCat_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        ProBusCat pbc = new ProBusCat();
        BusinessCataloge buscat = new BusinessCataloge();
        buscat.Id = Int32.Parse(this.grdListBusCat.SelectedRow.Cells[2].Text);
        Boolean bResult = pbc.DeleteBusCat(buscat);
        if (bResult == true)
        {
            this.BindGrid();
            this.lblError.Text = Constant.MESSAGE_DELETE_SUCCESS;
        }
        else
        {
            this.lblError.Text = Constant.MESSAGE_DELETE_ERROR;
        }
    }
    protected void grdListBusCat_RowEditing(object sender, GridViewEditEventArgs e)
    {
        BusinessCataloge buscat = new BusinessCataloge();
        buscat.Id = Int32.Parse(this.grdListBusCat.SelectedRow.Cells[2].Text);
        buscat.StrCatalogeName = this.grdListBusCat.SelectedRow.Cells[3].Text;
        this.txtNameofBusCat.Text = buscat.StrCatalogeName.ToString();
        this.btnSave.Visible = true;
        this.btnAdd.Visible = false;
    }
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        ProBusCat pbc = new ProBusCat();
        if (pbc.CheckBusCatExist(this.txtNameofBusCat.Text) == true) 
        {
            this.lblError.Text = Constant.MESSAGE_ERROR_NAMEBUSCAT_EXIST;
            this.txtNameofBusCat.Text = "";
            return;
        }
        if (this.txtNameofBusCat.Text.Trim() == null || this.txtNameofBusCat.Text.Trim() == "") 
        {
            this.lblError.Text = "Please input name of Business Cataloge";
            return;
        }

        BusinessCataloge bc = new BusinessCataloge();
        bc.StrCatalogeName = this.txtNameofBusCat.Text;
        Boolean bResult = pbc.SaveBusCataloge(bc);
        if (bResult == true)
        {
            this.BindGrid();
            this.txtNameofBusCat.Text = "";
            this.lblError.Text = Constant.MESSAGE_INSERT_SUCCESS;
        }
        else
        {
            this.lblError.Text = Constant.MESSAGE_INSERT_ERROR;
        }
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        //Update to DB BP
        ProBusCat pbc = new ProBusCat();
        BusinessCataloge buscat = new BusinessCataloge();
        buscat.Id = Int32.Parse(this.grdListBusCat.SelectedRow.Cells[2].Text);
        buscat.StrCatalogeName = this.txtNameofBusCat.Text;
        
        if (this.txtNameofBusCat.Text.Trim() == null || this.txtNameofBusCat.Text.Trim() == "")
        {
            this.lblError.Text = "Please input name of Business Cataloge";
            return;
        }
        Boolean bResult = pbc.UpdateBusCat(buscat);
        if (bResult == true)
        {
            this.txtNameofBusCat.Text = "";
            this.btnSave.Visible = false;
            this.lblError.Text = Constant.MESSAGE_UPDATE_SUCCESS;
            BindGrid();
            this.btnAdd.Visible = true;
        }
        else
        {
            this.lblError.Text = Constant.MESSAGE_UPDATE_ERROR;
        }
    }
    
}
